package com.lwm.lambda;

import com.lwm.entity.Employee;

import org.junit.Test;

import java.util.Comparator;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * lambda体中的内容有方法已经实现了，我们可以使用方法引用
 * @author liweimin
 * @version 1.0
 * @date 2020/9/7 9:28
 */
public class MethodRefTest {
    @Test
    public void test01(){
        Consumer<String> consumer = (x)-> System.out.println(x);
        Consumer<String> consumer1 = System.out::println;
        consumer1.accept("sfjsalfhsadklfhsajk");
    }
    @Test
    public void test02(){
        Employee employee = new Employee();
        Supplier<String> supplier = employee::getName;
        Supplier<String> supplier1 = employee::getName;
        Supplier<Integer> supplier2 = employee::getAge;
    }
    @Test
    public void test03(){

        Comparator<Integer> comparator = Integer::compare;
        System.out.println(comparator.compare(1, 4));
    }
    @Test
    public void test04(){
        BiPredicate<String, String> biPredicate1 = (x,y)->x.equals(y);
        /**
         * 类名::实例方法
         * 规则：（x，y）->x.equals(y);------------> x为实例方法的调用者，y为实例方法的参数时，可以String::equals;
         */
        BiPredicate<String, String> biPredicate = String::equals;
    }

    @Test
    public void test05(){
        /**
         * 构造器引用
         */
        Supplier<Employee> employeeSupplier = ()->new Employee();
        Supplier<Employee> employeeSupplier1 = Employee::new;
        System.out.println(employeeSupplier1.get().toString());
    } @Test
    public void test06(){
        /**
         * 参数列表保持一致
         */
        Function<String, Employee> function = (x)->new Employee(x);
        Function<String, Employee> function1 = Employee::new;
        System.out.println(function.apply("lwmzzz").toString());
    }@Test
    public void test07(){
        /**
         * 数组引用
         */
       Function<Integer, String[]> function = (x)->new String[x];
       Function<Integer, String[]> function1 = String[]::new;
        System.out.println(function1.apply(778).length);
        System.out.println(function.apply(45).length);
    }
}
